<?php

use \Mapper\MemberModel as MemberMapper;

class FriendcircleController extends \Base\AbstractController {
    /**
     * @desc 朋友圈列表
     */
    public function listAction() {
        $request = $this->getRequest();
        $class   = (int)$request->get('class');
        $keyword = trim((string)$request->get('keyword'));

        $memberMapper  = MemberMapper::getInstance();
        $supportMapper = \Mapper\FriendCircleSupportModel::getInstance();
        $commentMapper = \Mapper\CommentModel::getInstance();
        $where         = [];
        $ids           = [];
        $return        = [];

        if(!empty($class) || !empty($keyword)) {

            if($class === 1) {
                $where = ['member_id' => $keyword];

            } elseif($class === 2) {
                $memberModels = $memberMapper->fetchAll(['nick_name LIKE "%' . urlencode($keyword) . '%"']);

                foreach($memberModels as $memberModel) {
                    if($memberModel instanceof \MemberModel) {
                        $ids[] = $memberModel->getId();
                    }

                }

                $where = ['member_id' => $ids];
            }
        }

        $page = new \Lib\Paginator('friendCircle');
        $page->where($where);
        $page->order('id DESC');
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();

        foreach($models as $model) {
            $data                     = $model->toArray();
            $member                   = $memberMapper->findById($data['member_id']);
            $data['member_nick_name'] = urldecode($member->getNick_name());
            $data['support'] = $supportMapper->count(['friend_circle_id' => $data['id'], 'status' => 1]);
            $data['comment'] = $commentMapper->count(['cid' => $data['id'], 'type' => 1, 'status' => 1]);
            $return[]        = $data;
        }

        $this->assign('lists', $return);
    }

    /**
     * @desc 详细内容
     */
    public function editAction() {
        $request                  = $this->getRequest();
        $id                       = $request->get('id');
        $friendCircleModel        = \Mapper\FriendCircleModel::getInstance();
        $memberMapper             = MemberMapper::getInstance();
        $data                     = $friendCircleModel->findById($id)->toArray();
        $memberModel              = $memberMapper->findById($data['member_id']);

        if( $memberModel instanceof MemberModel) {
            $data['member_nick_name'] = urldecode($memberModel->getNick_name());
        }

        $this->assign('data', $data);
    }

    /**
     * @desc 屏蔽
     */
    public function delAction() {
        $request      = $this->getRequest();
        $id           = $request->get('id');
        $status       = $request->get('status');
        $friendCircle = \Mapper\FriendCircleModel::getInstance();

        if($status == 1) {
            $temp = 2;
        } elseif($status == 2) {
            $temp = 1;
        }

        try {
            $friendCircle->update(['status' => $temp], ['id' => $id]);
            return \Response::msg('操作成功');
        } catch(Exception $exc) {
            return \Response::errmsg($exc->getMessage());
        }

    }

    /**
     * @desc 朋友圈点赞
     */
    public function supportAction() {
        $request         = $this->getRequest();
        $circleId        = $request->get('circle_id');
        $where           = [];
        $where['status'] = 1;

        if(!empty($circleId)) {
            $where['friend_circle_id'] = $circleId;
        }

        $page = new \Lib\Paginator('friendCircleSupport');
        $page->where($where);
        $page->order('id DESC');
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models      = $page->lists();
        $memberModel = \Mapper\MemberModel::getInstance();
        $lists       = [];

        foreach($models as $model) {
            $data                     = $model->toArray();
            $member                   = $memberModel->findById($data['friend_id']);
            $data['member_nick_name'] = urldecode($member->getNick_name());
            $lists[]                  = $data;
        }

        $this->assign('lists', $lists);

    }
}
